<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Entities\Link;
use Modules\Admin\Http\Requests\LinkRequest;

class LinkController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $data = Link::all();
        return view('admin::link.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(LinkRequest $request)
    {
        $post = $request->all();
        Link::create($post);
        session()->flash('success','友链添加成功！');
        return redirect()->route('link.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(LinkRequest $request,$id)
    {
        $post = $request->all();
        $link = Link::find($id);
        //dd($link);
        $link['name'] = $post['name'];
        $link['url'] = $post['url'];
        $link['show'] = $post['show'];
        $link->save();
        session()->flash('success','友链添加成功！');
        return redirect()->route('link.index');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        Link::destroy($id);
        session()->flash('success','友链删除成功！');
        return back();
    }
}